package sg.edu.nus.iss.team2s.jass.io;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import sg.edu.nus.iss.team2s.jass.dto.JobAdvert;

/**
 * IO data access implementation for JobAdvert
 * @author luzhixian, Nie Xiaoyi
 * @version 1.0
 */

public class JobAdvertDataAccessImpl implements DataAccessI {

	@Override
	public List<Object> parseObjectsFromReader(BufferedReader in) throws IOException {
		List<Object> objList = new ArrayList<Object>();
		String lineStr;
		int lineCount = 0;
		JobAdvert objJobAdvert = null;
		String[] str = new String[15];
		while((lineStr = in.readLine()) != null){
			str[lineCount] = lineStr.substring(lineStr.indexOf("=")+1,lineStr.length());
			lineCount++;
			if(lineCount == str.length){
				try {
					objJobAdvert = new JobAdvert(str[0],str[1],str[2],str[3],str[4],str[5],str[6],(str[7].length() == 0)?null:sg.edu.nus.iss.team2s.jass.util.CommonUtility.parseToDate(str[7]==""?null:str[7],"yyyy-MM-dd"),str[8],str[9],str[10],str[11],(str[12].length() == 0)?null:sg.edu.nus.iss.team2s.jass.util.CommonUtility.parseToDate(str[12]==""?null:str[12],"yyyy-MM-dd"),(str[13].length() == 0)?null:sg.edu.nus.iss.team2s.jass.util.CommonUtility.parseToDate(str[13],"yyyy-MM-dd"),Integer.parseInt(str[14]));
				} catch (NumberFormatException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					throw new IOException("ParseException failed the IO", e);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					throw new IOException("Unexpected Exception failed the IO", e);
				}
				objList.add(objJobAdvert);
				lineCount = 0;
			}
		}
		return objList;
	}

	@Override
	public void writeObjectsToWriter(List<Object> objects,
			BufferedWriter out) throws IOException {
		int lineCount = 0;
		Iterator<Object> iterator = objects.iterator();
		while(iterator.hasNext()){
			JobAdvert objJobAdvert = (JobAdvert)iterator.next();
			if(objJobAdvert.getJobAdvertId() != null){
			out.write(recordStr("jobAvertId",lineCount,objJobAdvert.getJobAdvertId()));
			}
			else{
				out.write(recordStr("jobAvertId",lineCount,String.valueOf(lineCount)));
			}
			out.newLine();
			out.write(recordStr("jobTitle",lineCount,objJobAdvert.getJobTitle()));
			out.newLine();
			out.write(recordStr("jobDesc",lineCount,objJobAdvert.getJobDesc()));
			out.newLine();
			out.write(recordStr("techSkillList",lineCount,objJobAdvert.getTechSkillList()));
			out.newLine();
			out.write(recordStr("manageSkillList",lineCount,objJobAdvert.getManageSkillList()));
			out.newLine();
			out.write(recordStr("experience",lineCount,objJobAdvert.getExperience()));
			out.newLine();
			out.write(recordStr("salaryRange",lineCount,objJobAdvert.getSalaryRange()));
			out.newLine();
			if(objJobAdvert.getStartDate()!= null){
				out.write(recordStr("startDate",lineCount,sg.edu.nus.iss.team2s.jass.util.CommonUtility.parseFromDate(objJobAdvert.getStartDate(), "yyyy-MM-dd")));
			}
			else{
				out.write(recordStr("startDate",lineCount,null));
			}
			out.newLine();
			out.write(recordStr("location",lineCount,objJobAdvert.getLocation()));
			out.newLine();
			out.write(recordStr("companyID",lineCount,objJobAdvert.getCompanyId()));
			out.newLine();
			out.write(recordStr("department",lineCount,objJobAdvert.getDepartment()));
			out.newLine();
			out.write(recordStr("advertiserID",lineCount,objJobAdvert.getAdvertiserId()));
			out.newLine();
			if(objJobAdvert.getPostDate() != null){
				out.write(recordStr("postDate",lineCount,sg.edu.nus.iss.team2s.jass.util.CommonUtility.parseFromDate(objJobAdvert.getPostDate(), "yyyy-MM-dd")));
			}
			else{
				out.write(recordStr("postDate",lineCount,null));
			}
			out.newLine();
			if(objJobAdvert.getUpdateDate()!= null){
				out.write(recordStr("updateDate",lineCount,sg.edu.nus.iss.team2s.jass.util.CommonUtility.parseFromDate(objJobAdvert.getUpdateDate(), "yyyy-MM-dd")));
			}
			else{
				out.write(recordStr("updateDate",lineCount,null));
			}
			out.newLine();
			out.write(recordStr("status",lineCount,String.valueOf(objJobAdvert.getStatus())));
			out.newLine();
			lineCount++;
		}
	}
	
	private String recordStr(String fieldName,int count,String fieldValue)
	{
		if(fieldValue != null){
			return fieldName+count+"="+fieldValue;
		}
		else{
			return fieldName+count+"=";
		}
	}
}
